import Vue from 'vue'
import App from './App'
import store from './store'
Vue.config.productionTip = false

// 后端地址
const serviceUrl = 'http://192.168.43.94:8888';
const cdnPath = 'http://192.168.43.94/cdn/';
// websocket地址
const serverWss = "ws://192.168.43.94:8888/wss/im/";

// CDN图片资源
const cdn = {
	cdnPath: cdnPath,
	head_example: cdnPath + 'common/head_example.png',
	plaza_bg_example: cdnPath + 'common/plaza_bg.jpg',
	actCoverImg: cdnPath + 'actCoverImg/',
	appAvatar: cdnPath + "appAvatar/",
	actType: cdnPath + "actType/",
	actGroupQR: cdnPath + "actGroupQR/",
	ad: cdnPath + "ad/",
	photo: cdnPath + "photo/",
	faceImg: cdnPath + "faceImg/",
	groupTypeCover: cdnPath + "groupTypeCover/",
	groupCover: cdnPath + "groupCover/",
	groupQR: cdnPath + "groupQR/",
	groupDesc: cdnPath + "groupDesc/",
	plazzPhoto: cdnPath + "plazzPhoto/"
}

// 接口列表
const api = {
	pageSize: 10,
	// card左滑右滑
	cardPageSize: 8,
	getHomeData: '/api/home/getHomeData',
	getMoreActList: '/api/act/getMoreActList.short',
	getHotSearchList: '/api/home/getHotSearchList',
	pageActByActTypeId: '/api/act/pageActByActTypeId.short',
	searchByKey: '/api/searchRec/searchByKey',
	yetJoinActMember: '/api/member/yetJoinActMember',
	pageByNear: '/api/member/pageByNear.short',
	getMemberListRecommend: '/auth/api/member/getMemberListRecommend',
	pagePlazaContent: '/api/plazaContent/pagePlazaContent',
	deletePlaza: '/auth/api/plazaContent/deletePlaza',
	pagePlazaContentById: '/api/plazaContent/pagePlazaContentById',
	insertPlazaComment: '/auth/api/plazaComment/insertPlazaComment',
	pageGroup: '/api/group/pageGroup.short',
	pageGroupByMe: '/auth/api/group/pageGroupByMe',
	pageByNearMember: '/api/member/pageByNear.short',
	pageActNear: '/api/act/pageActNear.short',
	pageToMyMsg: '/auth/api/message/pageToMyMsg',
	pageToChat: '/auth/api/message/page',
	pageToMyJoinAct: '/auth/api/act/pageToMyJoinAct',
	pageToMePublish: '/auth/api/act/pageToMePublish'
}

const tui = {
	toast: function(text, duration, success) {
		uni.showToast({
			title: text,
			icon: success ? 'success' : 'none',
			duration: duration || 2000
		})
	},
	constNum: function() {
		const res = uni.getSystemInfoSync();
		return res.platform.toLocaleLowerCase() == "android" ? 300 : 0;
	},
	px: function(num) {
		return uni.upx2px(num) + 'px';
	},
	interfaceUrl: function() {
		//接口地址
		// return "http://192.168.8.161:8888/";
		return serviceUrl;
	},
	request: function(url, postData, method, type, hideLoading) {
		//接口请求
		if (!hideLoading) {
			uni.showLoading({
				mask: true,
				title: '请稍候...'
			})
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.interfaceUrl() + url,
				data: postData,
				header: {
					'content-type': type ? 'application/x-www-form-urlencoded' : 'application/json',
					'Authorization': this.getToken()
					// 是否启用跨域保护
					// 'security': 1
				},
				method: method, //'GET','POST'
				dataType: 'json',
				success: (res) => {
					!hideLoading && uni.hideLoading()
					if (res.data && res.data.code === 301 && !postData.modalAbate) {
						uni.showModal({
							title: '登录',
							content: '您尚未登录，请先登录',
							showCancel: false,
							confirmColor: "#5677FC",
							confirmText: '确定',
							success(e) {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}
						})
					} else {
						resolve(res.data)
					}
				},
				fail: (res) => {
					if (!hideLoading) {
						this.toast("网络不给力，请稍后再试~")
						uni.hideLoading()
					}
					reject(res)
				}
			})
		})
	},
	uploadFile: function(url, src, hideLoading) {
		const that = this

		if (hideLoading == undefined || hideLoading == null || hideLoading == '') {
			// 默认显示加载中
			hideLoading = false;
		}
		if (!hideLoading) {
			uni.showLoading({
				mask: true,
				title: '请稍候...'
			})
		}
		return new Promise((resolve, reject) => {
			const uploadTask = uni.uploadFile({
				url: this.interfaceUrl() + url,
				filePath: src,
				name: 'file',
				header: {
					// 'content-type': 'multipart/form-data',
					// 'content-type': type ? 'application/x-www-form-urlencoded' : 'application/json',
					'Authorization': this.getToken(),
					// 'Access-Control-Allow-Origin':'*'
				},
				formData: {},
				success: function(res) {
					/* uni.hideLoading();
					resolve(JSON.parse(res.data)); */

					!hideLoading && uni.hideLoading();
					let data = JSON.parse(res.data);
					if (data && data.code === 301) {
						uni.showModal({
							title: '登录',
							content: '您尚未登录，请先登录',
							showCancel: false,
							confirmColor: "#5677FC",
							confirmText: '确定',
							success(e) {
								uni.redirectTo({
									url: '/pages/login/login'
								})
							}
						})
					} else {
						resolve(JSON.parse(res.data));
					}

					/* 					let d = JSON.parse(res.data)
										if (d.code === 1) {
											let fileObj = JSON.parse(d.data)[0];
											//文件上传成功后把图片路径数据提交到服务器，数据提交成功后，再进行下张图片的上传
											resolve(fileObj)
										} else {
											that.toast(res.message);
										} */
				},
				fail: function(res) {
					// reject(res)
					// uni.hideLoading();
					// that.toast(res.message);

					if (!hideLoading) {
						this.toast("网络不给力，请稍后再试~")
						uni.hideLoading()
					}
					reject(res)
				}
			})
		})
	},
	geetWeek: function(dateStr) {
		let num = new Date(dateStr).getDay();
		if (num == 0) {
			return '周日'
		} else if (num == 1) {
			return '周一'
		} else if (num == 2) {
			return '周二'
		} else if (num == 3) {
			return '周三'
		} else if (num == 4) {
			return '周四'
		} else if (num == 5) {
			return '周五'
		} else if (num == 6) {
			return '周六'
		}
	},
	isBlank: function(str) {
		if (str == undefined || str == null || str == '') {
			return true;
		} else {
			return false;
		}
	},
	// 照片压缩设置大小
	reSizeImg: function(src, width, height, callback) {
		uni.getImageInfo({
			src,
			success(res) {
				console.log('压缩前', res)
				let canvasWidth = res.width //图片原始长宽
				let canvasHeight = res.height
				let img = new Image()
				img.src = res.path
				let canvas = document.createElement('canvas');
				let ctx = canvas.getContext('2d');
				// 未传则默认缩放
				if (width == null || height == null) {
					canvas.width = canvasWidth * 0.6;
					canvas.height = canvasHeight * 0.6;
				} else {
					canvas.width = width;
					canvas.height = height;
				}
				ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
				canvas.toBlob(function(fileSrc) {
					let imgSrc = window.URL.createObjectURL(fileSrc)
					console.log('压缩后', imgSrc);
					return callback(imgSrc);
				})
			}
		})
	},
	setToken: function(token) {
		uni.setStorageSync("token", token)
	},
	getToken() {
		return uni.getStorageSync("token")
	},
	isLogin: function() {
		return uni.getStorageSync("token") ? true : false
	}
}

Vue.prototype.tui = tui
Vue.prototype.$cdn = cdn
Vue.prototype.$api = api
Vue.prototype.$serverWss = serverWss
Vue.prototype.$eventHub = Vue.prototype.$eventHub || new Vue()
Vue.prototype.$store = store
App.mpType = 'app'

const app = new Vue({
	store,
	...App
})
app.$mount()
